bool IsOutlinePolite()
{ 
  int i;
  int thickness = int(cb_PostEffect_Advanced_Polite.u_FaceThickness);

  for(i=1; i <= thickness; i++)
  {
    if(  GetEffectTexture(s_EyeMapHL, vTexCoord, ivec2( 0,-i)).a == 1.0
      || GetEffectTexture(s_EyeMapHL, vTexCoord, ivec2( 0, i)).a == 1.0
      || GetEffectTexture(s_EyeMapHL, vTexCoord, ivec2( i, 0)).a == 1.0
      || GetEffectTexture(s_EyeMapHL, vTexCoord, ivec2(-i, 0)).a == 1.0)
    {
      return true;
    }
  }
   
  return false;
}


void main()
{
  vec4 col = GetEffectTexture(s_EyeMapHL, vTexCoord); //GetColor(vTexCoord);

  if (col.a == 1.0)
  {
    //--------------------------
    // Halo 

#ifdef MultiColorHighlight
    col.xyz = vec3(0, 0, 0);
#else
    col.xyz = cb_PostEffect_Advanced_Polite.u_HaloColor.xyz;
#endif

    col.a = 0;

    int i;
    for (i = 0; i < NUM_BLUR; i++)
    {
      vec4 value = GetEffectTexture(s_EyeMapHL, vTexCoord, cb_PostEffect_Advanced_Polite.u_HaloSize*g_FilterBlurData[i]);

#ifdef MultiColorHighlight
      if (value.a != 1.0)
      {
        col.rgb += value.rgb;
      }
#endif
      col.a += value.a != 1.0 ? 1.0 : 0.0; // Halo for visible and hidden geometry
    }

#ifdef MultiColorHighlight
    col.rgb /= col.a;
#endif

    col.a *= INV_NUM_BLUR * cb_PostEffect_Advanced_Polite.u_HaloIntensity;
  }
  else
  {
    if (IsOutlinePolite())
    {
      //--------------------------
      // Outline

#ifdef MultiColorHighlight
      col = vec4(col.rgb*1.142, 1.0);
#else
      col = cb_PostEffect_Advanced_Polite.u_OutlineColor;
#endif
    }
    else
    {
      //--------------------------
      // Politness
            
#ifndef MultiColorHighlight
      col.rgb = cb_PostEffect_Advanced_Polite.u_PolitnessColor.rgb;
#endif
      
      float zHL     = GetEffectTexture(s_ZMapHL, vTexCoord).x;
      float zScene  = GetZ(vTexCoord);
      if (zHL <= zScene)
      {
        // Visible
        float ratio = (col.a > 0.9) ? (1.0) : (1.0 - (col.a / 0.9) + (1 / 3.141));        
        col = vec4(col.rgb* ratio, cb_PostEffect_Advanced_Polite.u_VisibleFaceAlpha);
      }
      else
      {
        // Occluded
        col = vec4(col.rgb, cb_PostEffect_Advanced_Polite.u_OccludedAlpha);
      }
    }
  }

  FRAGCOLOR = col;
}
